unit uSearch;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, ADODB, StdCtrls, Buttons, ComCtrls, ExtCtrls, DBCtrls, DBLookupEh;

type
  TfrmSearch = class(TForm)
    SearchEdit: TEdit;
    SearchStatusBar: TStatusBar;
    SearchQuery: TADOQuery;
    DBNavigator1: TDBNavigator;
    SearchDataSource: TDataSource;
    SearchRG: TRadioGroup;
    SearchBtn: TButton;
    procedure SearchBtnClick(Sender: TObject);
    procedure DBNavigator1Click(Sender: TObject; Button: TNavigateBtn);
    procedure SearchRGClick(Sender: TObject);
  private
    { Private declarations }
    CB: TComboBox;
    LookupCB: TDBLookupComboboxEh;
    FindType: Byte;
  public
    { Public declarations }
    constructor Create(AOwner: TComponent; LookupCB: TDBLookupComboboxEh); reintroduce; overload;
    constructor Create(AOwner: TComponent; CB: TComboBox); reintroduce; overload;
  end;

var
  frmSearch: TfrmSearch;

implementation

uses uCompDM;

{$R *.dfm}

constructor TfrmSearch.Create(AOwner: TComponent; LookupCB: TDBLookupComboboxEh);
begin
  inherited Create(AOwner);

  Self.LookupCB := LookupCB;
  Self.FindType := 1;

  with SearchQuery do
    SQL[SQL.Count-3] := 'WHERE idPredpr = idPlat';
end;

constructor TfrmSearch.Create(AOwner: TComponent; CB: TComboBox);
begin
  inherited Create(AOwner);

  Self.CB := CB;
  Self.FindType := 0;

  with SearchQuery do
    SQL[SQL.Count-3] := 'WHERE idPlat <> (SELECT idPredpr FROM tbNastrPredpr) AND idPredpr = idPlat';
end;

procedure TfrmSearch.SearchBtnClick(Sender: TObject);
begin
  with SearchQuery do
  begin
    Close;

    case SearchRG.ItemIndex of
      0: SQL[SQL.Count-2] := 'AND namePredpr LIKE '+QuotedStr('%'+SearchEdit.Text+'%');
      1: SQL[SQL.Count-2] := 'AND UNN LIKE '+QuotedStr('%'+SearchEdit.Text+'%');
    end;

    Open;
    First;
  end;

  SearchStatusBar.SimpleText := SearchQuery.FieldByName('namePredpr').AsString;

  case Self.FindType of
    0: CB.ItemIndex := CB.Items.IndexOf(SearchQuery.FieldByName('namePredpr').AsString);
    1: LookupCB.KeyValue:=SearchQuery.FieldByName('idPredpr').Value;
  end;
end;

procedure TfrmSearch.SearchRGClick(Sender: TObject);
begin
  SearchEdit.SetFocus;
end;

procedure TfrmSearch.DBNavigator1Click(Sender: TObject; Button: TNavigateBtn);
begin
  SearchStatusBar.SimpleText := SearchQuery.FieldByName('namePredpr').AsString;

  case Self.FindType of
    0: CB.ItemIndex := CB.Items.IndexOf(SearchQuery.FieldByName('namePredpr').AsString);
    1: LookupCB.KeyValue:=SearchQuery.FieldByName('idPredpr').Value;
  end;
end;

end.
